package com.example.fieldnotes;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class Settings extends Activity {
	private SharedPreferences pref;
	private int sRow, eRow, sRange, eRange;
	private String sDate, eDate;
	private SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
	private TextView startRow, endRow;
	private Spinner ranges;
	private String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z"};
	private Button save, back;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		pref = getSharedPreferences("Options", 0);
		
		sRow = pref.getInt("sRow", 1);
		eRow = pref.getInt("eRow", 10);
		sRange = pref.getInt("sRange", 0);
		eRange = pref.getInt("eRange", 5) - 1;
		sDate = pref.getString("sDate", "01/01/1980");
		eDate = pref.getString("eDate", "12/31/2079");
		
		startRow = (TextView)findViewById(R.id.min);
		startRow.setText("" + sRow);
		endRow = (TextView)findViewById(R.id.max);
		endRow.setText("" + eRow);
		
		ranges = (Spinner)findViewById(R.id.spinner1);
		ArrayAdapter<?> phenoArrayAdapter = new ArrayAdapter<Object>(this,
		          android.R.layout.simple_spinner_item, alphabet);
		ranges.setAdapter(phenoArrayAdapter);
		ranges.setSelection(eRange);
		
		ranges.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				int i = 0;
				String temp = "";
				
				while (i < alphabet.length)
				{
					temp = ranges.getSelectedItem().toString();
					////System.out.println(temp.toString() + alphabet[i].toString());
					
					if (temp == alphabet[i])
					{
						eRange = i + 1;
						//System.out.println("found it" + eRange);
						break;
					}
					
					i++;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		setUpButtons();
	}
	
	public void setUpButtons() {
		save = (Button)findViewById(R.id.save);
		save.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sRow = Integer.parseInt(startRow.getText().toString());
				eRow = Integer.parseInt(endRow.getText().toString());
				saveOption();
			}
    	});
		
		back = (Button)findViewById(R.id.back);
		back.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				//TODO Auto-generated method stub
				Intent i = new Intent(Settings.this, MainActivity.class);
				finish();
				startActivity(i);
			}
		});
	}
	
	public void saveOption()
	{
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt("sRow", sRow);
		editor.putInt("eRow", eRow);
		editor.putInt("sRange", sRange);
		editor.putInt("eRange", eRange);
		editor.putString("sDate", sDate);
		editor.putString("eDate", eDate);
		editor.commit();
	}
}
